package com.ycxy.sh.transaction.controller.admin;


import com.ycxy.sh.transaction.mapper.NewsMapper;
import com.ycxy.sh.transaction.service.NewsService;
import com.ycxy.sh.transaction.util.KeyUtil;
import com.ycxy.sh.transaction.dao.vo.LayuiPageVo;
import com.ycxy.sh.transaction.dao.vo.PageVo;
import com.ycxy.sh.transaction.web.JsonResult;
import com.ycxy.sh.transaction.dao.entity.News;
import com.ycxy.sh.transaction.web.ServiceCode;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 *
 * @author hlt
 * @since 2019-12-21
 */
@Slf4j
@Api(tags = "8.公告管理模块")
@Controller
public class NewsController {
    @Autowired
    private NewsService newsService;

    @Autowired
    private NewsMapper newsMapper;

    /**
     * 发布公告
     * 1.传入公告标题（news_title），公告简介（news_describe）、公告内容（news_content），简介图（news_image）
     * 2.填写session获取的发布者
     * */
    @ResponseBody
    @PostMapping("/news/insert")
    public JsonResult insertNews(@RequestBody News news, HttpSession session){
        newsService.insertNews(news,session);
        log.debug("发布公告成功");
        return JsonResult.ok();
    }

    /**
     * 删除公告
     * 1.前端传入需删除公告的id
     * 2.判断是否是本人或超级管理员
     * */
    @ResponseBody
    @PutMapping("/news/delect/{id}")
    public JsonResult delectNews (@PathVariable("id") Long newsid, HttpSession session) {
        newsService.delectNews(newsid,session);
        log.debug("删除公告成功");
        return JsonResult.ok();
    }

    /**
     *查看公告详情
     * **/
    @GetMapping("/news/detail/{id}")
    public String queryNewsById (@PathVariable("id") Long newsid,ModelMap modelMap){
        News news = newsService.queryNewsById(newsid);
        if (StringUtils.isEmpty(news)){
            return "/error/404";
        }
        modelMap.put("news",news);
        return "/common/newsdetail";
    }
    /**
     *跳转到发布公告
     * **/
    @GetMapping("/news/torelnews")
    public String torelNews (){
        return "/admin/news/relnews";
    }

    /**
     * 跳转到修改公告
     * **/
    @GetMapping("/news/toupdate/{id}")
    public String toupDate (@PathVariable("id") Long newsId, ModelMap modelMap, HttpSession session){
        String username=(String) session.getAttribute("username");
        News news = newsMapper.selectNewsById(newsId);
        /**如果是本人则可以跳转修改*/
        if (news.getUsername().equals(username)){
            modelMap.put("qx",1);
            modelMap.put("news",news);
            return "/admin/news/updatenews";
        }
        modelMap.put("news",news);
        modelMap.put("qx",0);
        return "/admin/news/updatenews";
    }

    /**
     *修改公告
     * **/
    @ResponseBody
    @PostMapping("/news/update")
    public JsonResult updateNews (@RequestBody News news){
        newsService.updateNews(news);
        log.debug("公告修改成功！");
        return JsonResult.ok();
    }

    /**
     *查询前三条公告
     * **/
    @ResponseBody
    @GetMapping("/news/all")
    public JsonResult queryNews (){
        List<News> newsList = newsService.queryNews();
        log.debug("成功查询前三天公告");
        return JsonResult.ok();
    }

    /**
     * 后台分页查看公告列表
     * 1.前端传入页码、分页数量
     * 2.查询分页数据
     */
    @ResponseBody
    @GetMapping("/news/queryall")
    public LayuiPageVo queryAllNews(int limit, int page) {
        LayuiPageVo layuiPageVo = newsService.selectListNews(limit, page);
        log.debug("公告列表查询成功！");
        return layuiPageVo;
    }

    /**
     * 首页公告分页数据
     * */
    @GetMapping("/news/index/number")
    @ResponseBody
    public PageVo newsNumber(){
        int dataNumber = newsMapper.Count();
        return new PageVo(200,"查询成功",dataNumber);
    }

    /**
     * 首页网站公告
     * 1.前端传入页码、分页数量
     * 2.查询分页数据
     * */
    @GetMapping("/news/index/{page}")
    @ResponseBody
    public JsonResult<List<News>> newsIndex(@PathVariable("page") Integer page){
        List<News> newsList = newsService.selectHomePage(page);
        return JsonResult.ok(newsList);
    }

}